package br.com.ozsolucoes.sessionbeans;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import br.com.ozsolucoes.entity.Contas;
import br.com.ozsolucoes.entity.Servico;

@Stateless
public class ContasSessionBean implements Serializable {

	private static final long serialVersionUID = -6322475798665765915L;

	@PersistenceContext
	private EntityManager entityManager;

	private static final int STATUS_PENDENTE = 1;
	private static final int STATUS_PAGA = 2;
	private static final int STATUS_CANCELADA = 3;
	private static final int STATUS_ACERTO = 4;
	
	private static final int SERVICO_ATUAL = 1;

	public void registrarConta(Contas contas) {
		Servico servico = entityManager.find(Servico.class, contas.getServico().getId());
		contas.setServico(servico);
		contas.setId(0);
		entityManager.persist(contas);
	}

	public void cancelarContas(Contas conta) {
		merge(conta);
	}

	public void atualizarContas(Contas conta) {
		merge(conta);
	}

	public void merge(Object object) {
		entityManager.merge(object);
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasPendentes() {
		Query query = entityManager.createNamedQuery("Contas.consultarContasStatus");
		query.setParameter("status", STATUS_PENDENTE);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasPagas() {
		Query query = entityManager.createNamedQuery("Contas.consultarContasStatus");
		query.setParameter("status", STATUS_PAGA);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasCanceladas() {
		Query query = entityManager.createNamedQuery("Contas.consultarContasStatus");
		query.setParameter("status", STATUS_CANCELADA);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasAcerto() {
		Query query = entityManager.createNamedQuery("Contas.consultarContasStatus");
		query.setParameter("status", STATUS_ACERTO);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasClientePendentes(long idCliente) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasClienteStatus");
		query.setParameter("status", STATUS_PENDENTE);
		query.setParameter("idCliente", idCliente);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasClientePagas(long idCliente) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasClienteStatus");
		query.setParameter("status", STATUS_PAGA);
		query.setParameter("idCliente", idCliente);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasClienteCanceladas(long idCliente) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasClienteStatus");
		query.setParameter("status", STATUS_CANCELADA);
		query.setParameter("idCliente", idCliente);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasClienteAcerto(long idCliente) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasClienteStatus");
		query.setParameter("status", STATUS_ACERTO);
		query.setParameter("idCliente", idCliente);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasPendentesPeriodoPorCliente(Calendar dataInicial, Calendar dataFinal, long idCliente) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasClienteStatusPeriodo");
		query.setParameter("status", STATUS_PENDENTE);
		query.setParameter("idCliente", idCliente);
		query.setParameter("dataInicial", dataInicial.getTime());
		query.setParameter("dataFinal", dataFinal.getTime());
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasPagasPeriodoPorCliente(Calendar dataInicial, Calendar dataFinal, long idCliente) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasClienteStatusPeriodo");
		query.setParameter("status", STATUS_PAGA);
		query.setParameter("idCliente", idCliente);
		query.setParameter("dataInicial", dataInicial.getTime());
		query.setParameter("dataFinal", dataFinal.getTime());
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasCanceladasPeriodoPorCliente(Calendar dataInicial, Calendar dataFinal, long idCliente) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasClienteStatusPeriodo");
		query.setParameter("status", STATUS_CANCELADA);
		query.setParameter("idCliente", idCliente);
		query.setParameter("dataInicial", dataInicial.getTime());
		query.setParameter("dataFinal", dataFinal.getTime());
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasAcertoPeriodoPorCliente(Calendar dataInicial, Calendar dataFinal, long idCliente) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasClienteStatusPeriodo");
		query.setParameter("status", STATUS_ACERTO);
		query.setParameter("idCliente", idCliente);
		query.setParameter("dataInicial", dataInicial.getTime());
		query.setParameter("dataFinal", dataFinal.getTime());
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasPendentesPeriodo(Calendar dataInicial, Calendar dataFinal) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasStatusPeriodo");
		query.setParameter("status", STATUS_PENDENTE);
		query.setParameter("dataInicial", dataInicial.getTime());
		query.setParameter("dataFinal", dataFinal.getTime());
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasPagasPeriodo(Calendar dataInicial, Calendar dataFinal, long idCliente) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasStatusPeriodo");
		query.setParameter("status", STATUS_PAGA);
		query.setParameter("dataInicial", dataInicial.getTime());
		query.setParameter("dataFinal", dataFinal.getTime());
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasCanceladasPeriodo(Calendar dataInicial, Calendar dataFinal) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasStatusPeriodo");
		query.setParameter("status", STATUS_CANCELADA);
		query.setParameter("dataInicial", dataInicial.getTime());
		query.setParameter("dataFinal", dataFinal.getTime());
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasAcertoPeriodo(Calendar dataInicial, Calendar dataFinal) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasStatusPeriodo");
		query.setParameter("status", STATUS_ACERTO);
		query.setParameter("dataInicial", dataInicial.getTime());
		query.setParameter("dataFinal", dataFinal.getTime());
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasClientePeriodoPorCliente(Calendar dataInicial, Calendar dataFinal, long idCliente) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasClientePeriodo");
		query.setParameter("idCliente", idCliente);
		query.setParameter("dataInicial", dataInicial.getTime());
		query.setParameter("dataFinal", dataFinal.getTime());
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasPeriodo(Calendar dataInicial, Calendar dataFinal) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasClientePeriodo");
		query.setParameter("dataInicial", dataInicial.getTime());
		query.setParameter("dataFinal", dataFinal.getTime());
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarTodasContasCliente(long idCliente) {
		Query query = entityManager.createNamedQuery("Contas.consultarTodasContasCliente");
		query.setParameter("idCliente", idCliente);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarTodasContas() {
		Query query = entityManager.createNamedQuery("Contas.consultarTodasContas");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasPendentesClientePorServico(long idCliente, long idServico) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasClienteStatusServico");
		query.setParameter("status", STATUS_PENDENTE);
		query.setParameter("statusServico", SERVICO_ATUAL);
		query.setParameter("idCliente", idCliente);
		query.setParameter("idServico", idServico);

		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasPagasClientePorServico(long idCliente, long idServico) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasClienteStatusServico");
		query.setParameter("status", STATUS_PAGA);
		query.setParameter("statusServico", SERVICO_ATUAL);
		query.setParameter("idCliente", idCliente);
		query.setParameter("idServico", idServico);

		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasAcertoClientePorServico(long idCliente, long idServico) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasClienteStatusServico");
		query.setParameter("status", STATUS_ACERTO);
		query.setParameter("statusServico", SERVICO_ATUAL);
		query.setParameter("idCliente", idCliente);
		query.setParameter("idServico", idServico);

		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("deprecation")
	public Contas consultarContaClientePorServicoPeriodo(long idCliente, long idServico, Date dataIncial) {
		Date dataFinal = dataIncial;
		dataFinal.setMonth(dataIncial.getMonth() - 1);
		Query query = entityManager.createNamedQuery("Contas.consultarContaClientePorServicoPeriodo");
		query.setParameter("idCliente", idCliente);
		query.setParameter("idServico", idServico);
		query.setParameter("dataIncial", dataIncial);
		query.setParameter("dataFinal", dataFinal);
		try {
			return (Contas) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings({ "deprecation", "static-access" })
	public Contas consultarUltimaContaCliente(long idCliente) {
		Date dataInicial = new Date();
		Date dataFinal = new Date();
		Calendar calendar = Calendar.getInstance();
		if (dataInicial.getDate() >= 9) {
			dataInicial.setMonth(dataInicial.getMonth() - 1);
			dataFinal.setMonth(dataInicial.getMonth());
		}
		dataInicial.setDate(1);
		dataFinal.setDate(calendar.getActualMaximum(calendar.DAY_OF_MONTH));

		Query query = entityManager.createNamedQuery("Contas.consultarContasClientePeriodo");
		query.setParameter("idCliente", idCliente);
		query.setParameter("dataInicial", dataInicial);
		query.setParameter("dataFinal", dataFinal);
		try {
			return (Contas) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Contas> consultarContasComComprovantesPendente(long idCliente, long idServico) {
		Query query = entityManager.createNamedQuery("Contas.consultarContasComComprovantesStatus");
		query.setParameter("idCliente", idCliente);
		query.setParameter("idServico", idServico);
		query.setParameter("status", 1);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
}